// pages/favoriteSongs/favoriteSongs.js
const utils = require('../../utils/util.js');
Page({
  data: {
    LoveList: [],
    PlayList: [],
    PlayListVisible: false
  },

  onLoad(options) {
    this.setData({
      PlayList: wx.getStorageSync('MusicFromData'),
      LoveList: wx.getStorageSync('LoveList')
    })
  },

  onShow() {
    this.setData({
      PlayList: wx.getStorageSync('MusicFromData'),
      LoveList: wx.getStorageSync('LoveList')
    })
  },

  //添加至歌单
  PlayListAdd:function(e) {
    let id = e.currentTarget.dataset.sId;
    let selectedSong = this.data.LoveList.find(item => item.songMid === id);
    
    if (selectedSong) {
      if(!selectedSong.isAdded) {
        utils.addToPlaylist(selectedSong.songMid);
        console.log("添加成功");
      } else {
        console.log("歌曲已存在歌单中");
      }
    };
    this.setData({
      PlayList:this.data.PlayList
    })
  },

  // ChangeLoveList:function(e) {
  //   let id = e.currentTarget.dataset.sId;
  //   utils.LovelistChange(id);
  //   this.triggerEvent('LovelistSongChange', { sId: id });
  //   console.log("改变收藏夹成功");
  //   this.setData({
  //     LoveList:this.data.LoveList
  //   })
  // },

  OpenPlayList:function() {
    this.setData({
      PlayListVisible: true
    });
  },
  ClosePlayList:function() {
    this.setData({
      PlayListVisible: false
    });
  },

  To_Create_Room:function() {
    wx.navigateTo({
      url: '/pages/createroom/createroom',
    })
  }

})